package com.neuedu.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.neuedu.model.FangChan;
import com.neuedu.model.XiaoQu;
import com.neuedu.service.FangChanService;
import com.neuedu.service.impl.FangChanServiceImpl;
import com.neuedu.utils.R;
import com.neuedu.vo.PageVO;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import static com.neuedu.controller.UserController.FILE_PATH;

public class FangChanController {
    private FangChanService fangChanService = new FangChanServiceImpl();

    public R page(JSONObject jsonObject){
        Integer pageNo=jsonObject.getInteger("pageNo");
        Integer pageSize=jsonObject.getInteger("pageSize");
        //在某两个日期之间，like 一个给定的小区名称。
        String startTime=jsonObject.getString("startTime");
        String endTime=jsonObject.getString("endTime");
        String name=jsonObject.getString("name");
        //where startTime<=创建时间 and endTime>=创建时间 and name like '%金华%' limit (pageNo-1)*pageSize,pageSize
        PageVO<FangChan> pageVO = fangChanService.selectOnePage(pageNo, pageSize, startTime, endTime, name);
        return R.ok().setData(pageVO);
    }
    public void showImage(HttpServletRequest request, HttpServletResponse response){
        String fileName = request.getParameter("fileName");
        File file=new File(FILE_PATH+File.separator+fileName);
        try {
            BufferedImage read = ImageIO.read(file);
            ImageIO.write(read,"png",response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public R del(JSONObject jsonObject){
        JSONArray delId = jsonObject.getJSONArray("delId");
        Integer [] deleteId=new Integer[delId.size()];
        for (int i = 0; i < delId.size(); i++) {
            deleteId[i]=delId.getInteger(i);
        }
        int result = fangChanService.del(deleteId);
        if(result>0){
            return R.ok("删除成功");
        }else{
            return R.error("删除失败");
        }
    }

    public R add(JSONObject jsonObject){
        // 拆大json
        Integer id = jsonObject.getInteger("id");
        String xiaoqumingcheng = jsonObject.getString("xiaoqumingcheng");
        String dongshu = jsonObject.getString("dongshu");
        String bianhao = jsonObject.getString("bianhao");
        String fangchanmingcheng = jsonObject.getString("fangchanmingcheng");
        String huzhumingcheng = jsonObject.getString("huzhumingcheng");
        String telephone = jsonObject.getString("telephone");
        Integer fangjianshu = jsonObject.getInteger("fangjianshu");
        Integer danyuanhao = jsonObject.getInteger("danyuanhao");
        Integer loucenghao = jsonObject.getInteger("loucenghao");
        String description = jsonObject.getString("description");
        String ruzhushijian = jsonObject.getString("ruzhushijian");
        // 创建房产对象
        FangChan fangChan = new FangChan();
        // 把房产列表对象填充
        fangChan.setId(id);
        fangChan.setXiaoqumingcheng(xiaoqumingcheng);
        fangChan.setDongshu(dongshu);
        fangChan.setBianhao(bianhao);
        fangChan.setFangchanmingcheng(fangchanmingcheng);
        fangChan.setHuzhumingcheng(huzhumingcheng);
        fangChan.setTelephone(telephone);
        fangChan.setFangjianshu(fangjianshu);
        fangChan.setDanyuanhao(danyuanhao);
        fangChan.setLoucenghao(loucenghao);
        fangChan.setDescription(description);
        fangChan.setRuzhushijian(ruzhushijian);
        //调用service，录入一个新的房产信息
        int result = fangChanService.add(fangChan);
        if(result>0){
            return R.ok("添加成功");
        }else{
            return R.error("添加失败");
        }
    }

    public R edit(JSONObject jsonObject){
        String s = jsonObject.toJSONString();
        FangChan fangChan = jsonObject.parseObject(s,FangChan.class);
        int result = fangChanService.editFangChanInfo(fangChan);
        if(result>0){
            return R.ok("更新成功");
        }else{
            return R.error("更新失败");
        }
    }

    public R showFangChan(JSONObject jsonObject){
        Integer id = jsonObject.getInteger("id");
        FangChan fangChan = fangChanService.findFangChanById(id);
        return R.ok().setData(fangChan);
    }
    public R selectFangChanAll(Map<String,String[]> map){
        List<FangChan> fangChans = fangChanService.selectFangChanAll();
        return R.ok().setData(fangChans);
    }

}
